import { PureComponent } from 'react';
import { Cascader } from 'antd';


class AsyncThreeLevelLinkAge extends PureComponent {

	state = {
    	cascaderData: "",
    	cascaderValue: "",
    }

	onChange = (value, selectedOptions) => {
	  	const { selectedCascaderValue } = this.props;
	  	selectedCascaderValue(selectedOptions);
	    
	};
	
	constructor(props) {
	    super(props);
	    this.state = {
	    	cascaderData: props.geographicData.list,
	    	cascaderValue: props.cascaderValues,
	    }
	}
	
  loadData = selectedOptions => {
    const targetOption = selectedOptions[selectedOptions.length - 1];
    targetOption.loading = true;
    const { dispatch,requestModelName } = this.props;
    const params = {
      parentid: targetOption.key,
    };
    dispatch({
        type: requestModelName + "/get_cascader_areatree_by_parentid",
        payload: params,
        callback: () => {
          const { geographicData: { list } } = this.props;
          targetOption.children = list;
          this.setState({
          	cascaderData: [...this.state.cascaderData],
          });
          targetOption.loading = false;
        }
    });
  };

  render() {
    return (
      <Cascader
      	defaultValue={this.state.cascaderValue}
        options={this.state.cascaderData}
        loadData={this.loadData}
        onChange={this.onChange}
        placeholder="请选择省市县"
        changeOnSelect
      />
    );
  }
}

export default AsyncThreeLevelLinkAge;